package fu.capstone.hanoicityguide.presentation.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.model.CommonDialogue;
import fu.capstone.hanoicityguide.common.utility.CacheCommonDialogues;

public class ListCommonDialogueAdapter extends ArrayAdapter<CommonDialogue> {

	private Activity context = null;
	private int resource;
	private ArrayList<CommonDialogue> arr = null;
	private Button btnPlay;
	private TextView english;
	private TextView vietnamese;
	private CacheCommonDialogues cacheCommonDialogue;
	private CommonDialogue cDialogue;

	public ListCommonDialogueAdapter(Activity context, int resource,
			ArrayList<CommonDialogue> objects) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.resource = resource;
		this.arr = objects;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		LayoutInflater inflater = context.getLayoutInflater();
		convertView = inflater.inflate(resource, null);
		cDialogue = arr.get(position);
		cacheCommonDialogue = new CacheCommonDialogues();

		vietnamese = (TextView) convertView.findViewById(R.id.vietnameseTitle);
		english = (TextView) convertView.findViewById(R.id.englishTitle);
		btnPlay = (Button) convertView.findViewById(R.id.btnPlay);
		vietnamese.setText(cDialogue.getVietnameseContent());
		english.setText(cDialogue.getEnglishContent());

		btnPlay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// btnPlay.setTag(position, cDialogue);
				cacheCommonDialogue.startPlaying(context,
						"audio" + (arr.get(position)).getId());
				Log.v("playing", "" + position);
			}
		});

		return convertView;
	}

	public void setArr(ArrayList<CommonDialogue> arr) {
		this.arr = arr;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arr.size();
	}

}
